package com.smit.icast;

import android.content.Context;
import android.content.Intent;

import com.smit.dvb.DVBManager;
import com.smit.dvb.DVBService;
import com.smit.dvb.IDVBCallback;
import com.smit.dvb.ci.CIControl;
import com.smit.dvb.ci.ICICallback;
import com.smit.dvb.epg.EpgControl;
import com.smit.dvb.epg.IEpgCallback;
import com.smit.dvb.scan.IScanCallback;
import com.smit.dvb.scan.ScanControl;
import com.smit.dvb.service.IServiceCallback;
import com.smit.dvb.service.ServiceControl;
import com.smit.dvb.setup.ISetupCallback;
import com.smit.dvb.setup.SetupControl;
import com.smit.dvb.system.ISystemCallback;
import com.smit.dvb.system.SystemControl;
import com.smit.dvb.update.IUpdateCallback;
import com.smit.dvb.update.UpdateControl;
import com.smit.utils.LogUtil;

import java.util.List;

/**
 * icast dvb 管理类
 */
public class ICastDVBManager extends DVBManager implements IDVBCallback {
    private static final String TAG = "ICastDVBManager";
    private Context context;
    private ICastDVBService icastDVBService;
    private CIControl ciControl;
    private EpgControl epgControl;
    private ScanControl scanControl;
    private ServiceControl serviceControl;
    private SetupControl setupControl;
    private SystemControl systemControl;
    private UpdateControl updateControl;
    private FirmwareUpdate firmwareUpdate;
    /**
     * 构造函数
     * @param context Android application context
     */
    public ICastDVBManager(Context context) {
        this.context = context;
        icastDVBService = new ICastDVBService();
        icastDVBService.initialize();
        startAppService(context);
        ciControl = new CIControl();
        epgControl = new EpgControl();
        scanControl = new ScanControl();
        serviceControl = new ServiceControl();
        setupControl = new SetupControl();
        systemControl = new SystemControl();
        updateControl = new UpdateControl();
        firmwareUpdate = new FirmwareUpdate(context);
    }

    /**
     * 启动service
     * @param context 环境变量
     */
    private void startAppService(Context context)
    {
        Intent intent = new Intent(context, ICastAppService.class);
        intent.putExtra("FROM", "ICast");
        context.startService(intent);
    }

    /**
     * 初始化DVB Manager
     */
    @Override
    public void initialize() {
        LogUtil.info(TAG, "initialize start>>>");
        icastDVBService.registerDVBCallback(this);
        updateControl.addCallback(firmwareUpdate);
    }

    /**
     * 释放DVB Manager
     */
    @Override
    public void release() {
        LogUtil.info(TAG, "release start>>>");
        icastDVBService.removeDVBCallback();
        updateControl.removeCallback(firmwareUpdate);
    }

    /**
     * 获取DVB Service
     * @return DVB Service
     */
    @Override
    public DVBService getDVBService() {
        return icastDVBService;
    }

    /**
     * 获取CI控制
     * @return CIControl 对象
     * @see com.smit.dvb.ci.CIControl
     */
    @Override
    public CIControl getCIControl() {
        return ciControl;
    }

    /**
     * 获取Epg控制
     * @return EpgControl 对象
     * @see com.smit.dvb.epg.EpgControl
     */
    @Override
    public EpgControl getEpgControl() {
        return epgControl;
    }

    /**
     * 获取搜台控制
     * @return ScanControl 对象
     * @see com.smit.dvb.scan.ScanControl
     */
    @Override
    public ScanControl getScanControl() {
        return scanControl;
    }

    /**
     * 获取Service控制
     * @return ServiceControl 对象
     * @see com.smit.dvb.service.ServiceControl
     */
    @Override
    public ServiceControl getServiceControl() {
        return serviceControl;
    }

    /**
     * 获取Setup控制
     * @return SetupControl 对象
     * @see com.smit.dvb.setup.SetupControl
     */
    @Override
    public SetupControl getSetupControl() {
        return setupControl;
    }

    /**
     * 获取System控制
     * @return SystemControl 对象
     * @see com.smit.dvb.system.SystemControl
     */
    @Override
    public SystemControl getSystemControl() {
        return systemControl;
    }

    /**
     * 获取升级控制
     * @return UpdateControl 对象
     * @see com.smit.dvb.update.UpdateControl
     */
    @Override
    public UpdateControl getUpdateControl() {
        return updateControl;
    }

    @Override
    public void handleCICallbackMsg(int msg, Object param) {
        List<ICICallback> callbackList = ciControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (ICICallback callback : callbackList) {
                callback.handleCICallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleEpgCallbackMsg(int msg, Object param) {
        List<IEpgCallback> callbackList = epgControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (IEpgCallback callback : callbackList) {
                callback.handleEpgCallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleScanCallbackMsg(int msg, Object param) {
        List<IScanCallback> callbackList = scanControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (IScanCallback callback : callbackList) {
                callback.handleScanCallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleServiceCallbackMsg(int msg, Object param) {
        List<IServiceCallback> callbackList = serviceControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (IServiceCallback callback : callbackList) {
                callback.handleServiceCallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleSetupCallbackMsg(int msg, Object param) {
        List<ISetupCallback> callbackList = setupControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (ISetupCallback callback : callbackList) {
                callback.handleSetupCallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleSystemCallbackMsg(int msg, Object param) {
        List<ISystemCallback> callbackList = systemControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (ISystemCallback callback : callbackList) {
                callback.handleSystemCallbackMsg(msg, param);
            }
        }
    }

    @Override
    public void handleUpdateCallbackMsg(int msg, Object param) {
        List<IUpdateCallback> callbackList = updateControl.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (IUpdateCallback callback : callbackList) {
                callback.handleUpdateCallbackMsg(msg, param);
            }
        }
    }
}